//
//  UITableViewCell+Custom.m
//  TestTableView
//
//  Created by 陈柏兵 on 16/5/4.
//  Copyright © 2016年 K. All rights reserved.
//

#import "UITableViewCell+Custom.h"
#import <objc/runtime.h>

static const void *ModelKey = &ModelKey;

@implementation UITableViewCell (Custom)
@dynamic model;

- (id<CustomCellModel>)model{
    return objc_getAssociatedObject(self, ModelKey);
}

- (void)setModel:(id<CustomCellModel>)model{
    objc_setAssociatedObject(self, ModelKey, model, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (UITableViewCell *)tableView:(UITableView *)tableView cellWithModel:(id<CustomCellModel>) model{
    NSString *className = model.className;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:className];
    
    if (cell == nil) {
        Class class = NSClassFromString(className);
        cell = [[class alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:className];
    }
    
    cell.model = model;
    return cell;
}

@end
